/* $RCSFile: StringLocalizer.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/StringLocalizer/StringLocalizer.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:22  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:44:00  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Web.Services.Protocols;

using AddonNice.UI;
using AddonNice.Design;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings;
using AddonNice.Authentication;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.StringLocalizer 
{

 	#region Local TraceSwitch 
	public class StringLocalizerTrace : ADNTraceSwitch
	{
		static public ADNTraceSwitch	Sw	            =	new ADNTraceSwitch("StringLocalizerTrace","Special Trace Switch for StringLocalizer");
	}
	#endregion Local TraceSwitch 

    #region const strings
    public class LStr
    {
		public const string StringLocalizerEntity  	        =	"MODULE_SETTINGS_FIELD_LOC_ENTITY";
		public const string StringLocalizerFields  	        =	"MODULE_SETTINGS_FIELD_LOC_FIELDS";
        public const string StringLocalizerNbTabs             =	"MODULE_SETTINGS_FIELD_LOC_NBTABS";
        public const string StringLocalizerTabsOrientation    =	"MODULE_SETTINGS_FIELD_LOC_TABSORIENTATION";
        public const string GoogleTranslate                     =	"MODULE_SETTINGS_FIELD_LOC_GOGGLETRANSLATE";
        public const string StringLocalizerMemoLen            =	"MODULE_SETTINGS_FIELD_LOC_MEMOLEN";
        public const string StringLocalizerTextLen            =	"MODULE_SETTINGS_FIELD_LOC_TEXTLEN";
        public const string StringLocalizerMemoSize           =	"MODULE_SETTINGS_FIELD_LOC_MEMOSIZE";
        public const string StringLocalizerTextSize           =	"MODULE_SETTINGS_FIELD_LOC_TEXTSIZE";
        public const string StringLocalizerMemoHTML           =	"MODULE_SETTINGS_FIELD_LOC_MEMOEDITOR";
            
    }
    #endregion const strings
    
	/// <summary>
    /// Initializer class for StringLocalizer 
    /// </summary>
    public class StringLocalizerSI : PortalModuleControlSI
    {
        public StringLocalizerSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]StringLocalizerSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]StringLocalizerSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]StringLocalizerSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);
            //const string extgrp                         =   "STRINGLOCALIZER";
            
        }
    }

    
    /// <summary>
    ///	StringLocalizer display prospects in a RadGrid.
    /// </summary>
	public partial class StringLocalizer : PortalModuleControl
    {
 		#region General implementation

        /// <summary>
		/// AddonNice module key
		/// </summary>
		public override Guid GuidID
        {
            get
            {
               return new Guid("{12974FEC-8EAB-4db1-8514-DA31924E0F47}");
            }
        }
		#endregion General implementation

        #region Local Properties


        StringLocalizerDB fDB                                    =   new StringLocalizerDB();

        #endregion local Properties

        #region Ctor and Initializer

        /// <summary>
        /// Ctor allows settings to select an entity and its fields to localize
        /// </summary>
        public StringLocalizer()
        {
            if ( StringLocalizerTrace.Sw.TraceVerbose || ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]StringLocalizer Ctor BEG",(StringLocalizerTrace.Sw.TraceVerbose ) ? StringLocalizerTrace.Sw.Info :ModuleTraceSwitch.Sw.Info);
		

            if ( StringLocalizerTrace.Sw.TraceVerbose || ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]StringLocalizer Ctor END",(StringLocalizerTrace.Sw.TraceVerbose) ? StringLocalizerTrace.Sw.Info:ModuleTraceSwitch.Sw.Info);
        }

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new StringLocalizerSI(bSettings,this);
        }

        #endregion Ctor

        
  
        protected void Page_Load(object sender, EventArgs e)
        {
           if ( StringLocalizerTrace.Sw.TraceVerbose || ModuleTraceSwitch.Sw.TraceVerbose )
               Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizer Page_Load BEG IsPostBack: {0}, IsInAsyncPostBack: {1}",IsPostBack,IsInAsyncPostBack),( StringLocalizerTrace.Sw.TraceVerbose ) ? StringLocalizerTrace.Sw.Info :ModuleTraceSwitch.Sw.Info);
           
           if ( !IsPostBack )
           {

           }
           if ( StringLocalizerTrace.Sw.TraceVerbose || ModuleTraceSwitch.Sw.TraceVerbose )
               Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizer Page_Load END IsPostBack: {0}",IsPostBack),(StringLocalizerTrace.Sw.TraceVerbose)? StringLocalizerTrace.Sw.Info:ModuleTraceSwitch.Sw.Info);
         }

        #region Main methods

        void RadGridResources_NeedDataSource(object source,GridNeedDataSourceEventArgs e)
        {
            string cultCode             =   portalSettings.PortalContentLanguage.TwoLetterISOLanguageName;
            RadGridResources.DataSource =   fDB.GetAllStringResourceByCountry("ADNCRM",cultCode);
        }

        void RadGridResources_DataBound(object sender,EventArgs e)
        {
            if ( RadGridResources.DataSource is SqlDataReader )
                ((SqlDataReader)RadGridResources.DataSource).Close();
        }

        void RadGridResources_UpdateCommand(object source, GridCommandEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizer RadGridResources_UpdateCommand BEG Item: {0}",e.Item),ModuleTraceSwitch.Sw.Info);
            GridEditableItem editedItem     =   e.Item as GridEditableItem;
            Hashtable newValues             =   new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            string resourceType             =   (string)RadGridResources.MasterTableView.DataKeyValues[editedItem.ItemIndex]["resourceType"];
            string cultureCode              =   (string)RadGridResources.MasterTableView.DataKeyValues[editedItem.ItemIndex]["cultureCode"];
            string resourceKey              =   (string)RadGridResources.MasterTableView.DataKeyValues[editedItem.ItemIndex]["resourceKey"];

            try
            {
                string resourceValue        =   (string)newValues["resourceValue"];
                fDB.AddStringResource(resourceType,cultureCode,resourceKey,resourceValue);
		        if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizer RadGridResources_UpdateCommand END resourceType: {0},cultureCode: {1},resourceKey: {2},resourceValue: {3}",
                        new object[]{resourceType,cultureCode,resourceKey,resourceValue}),ModuleTraceSwitch.Sw.Info);
            }
            catch (Exception ex)
            {
			    if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]StringLocalizer RadGrid1_UpdateCommand ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }

        void RadGridResources_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizer RadGridResources_InsertCommand BEG Item: {0}",e.Item),ModuleTraceSwitch.Sw.Info);
            GridEditableItem editedItem     =   e.Item as GridEditableItem;
            Hashtable newValues             =   new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            try
            {
                string resourceType         =   (newValues.ContainsKey("resourceType")) ? (string)newValues["resourceType"]:"ADNCRM";
                string cultureCode          =   (newValues.ContainsKey("cultureCode")) ?(string)newValues["cultureCode"]:portalSettings.PortalContentLanguage.TwoLetterISOLanguageName;
                string resourceKey          =   (newValues.ContainsKey("resourceKey")) ?(string)newValues["resourceKey"]:string.Empty;

                string resourceValue        =   (string)newValues["resourceValue"];
                if ( resourceKey != string.Empty )
                    fDB.AddStringResource(resourceType,cultureCode,resourceKey,resourceValue);
		        if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizer RadGridResources_InsertCommand END resourceType: {0},cultureCode: {1},resourceKey: {2},resourceValue: {3}",
                        new object[]{resourceType,cultureCode,resourceKey,resourceValue}),ModuleTraceSwitch.Sw.Info);
            }
            catch (Exception ex)
            {
			    if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]StringLocalizer RadGridResources_InsertCommand ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }


        #endregion Main methods


        #region Std Overrides

        override protected void OnInit(EventArgs e)
        {
            if ( StringLocalizerTrace.Sw.TraceVerbose || ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]StringLocalizer OnInit BEG.",(StringLocalizerTrace.Sw.TraceVerbose) ? StringLocalizerTrace.Sw.Info:ModuleTraceSwitch.Sw.Info);
            InitializeComponent();
            base.OnInit(e);
            if ( StringLocalizerTrace.Sw.TraceVerbose || ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]StringLocalizer OnInit END.",(StringLocalizerTrace.Sw.TraceVerbose) ? StringLocalizerTrace.Sw.Info:ModuleTraceSwitch.Sw.Info);
        }
 

        private void InitializeComponent()
        {
            Load                            +=  new System.EventHandler(this.Page_Load);
            RadGridResources.NeedDataSource +=  new GridNeedDataSourceEventHandler(RadGridResources_NeedDataSource);
            RadGridResources.DataBound      +=  new EventHandler(RadGridResources_DataBound);
            RadGridResources.InsertCommand  +=  new GridCommandEventHandler(RadGridResources_InsertCommand);
            RadGridResources.UpdateCommand  +=  new GridCommandEventHandler(RadGridResources_UpdateCommand);
        }


        #endregion
        
    }
}